<?php
/**
 * 菜单控制器
 * -提供了后台菜单的增删改功能
 *
 * @author
 * @version
 */
class MenuAction extends AbstractAction
{
	/**
	 * @var MenuModel
	 */
    protected $Menu = NULL;

	/**
	 * 控制器预初始化
	 *
	 * @return NULL
	 */
    protected function _initialize()
    {
		parent::_initialize();
        $this->Menu = D('Admin/Menu');
    }

    /**
	 * 菜单管理首页
	 *
	 * @return HTML
	 */
	public function index()
	{
        $parentid = isset($_GET['parentid']) ? intval($_GET['parentid']) : null;
        $data = $this->Menu->getChild($parentid);
        $data = json_encode($data);

        $this->assign('parentid', $parentid);
        $this->assign('head', array('title'=>'菜单'));
        $this->assign('data', $data);
		$this->display();
	}

	/**
	 * 添加菜单
	 *
	 * @return HTML & JSON
	 */
    public function add()
    {
        if ($this->isPost()) {
			$result = false;
			// 验证创建对象
			if ($data = $this->Menu->create()) {
				// 新增
				if ($menuid = $this->Menu->add($data)) {
					$result = true;
					$row = $this->Menu->find($menuid);
				}
			}
            echo json_encode(array('state'=>$result, 'data'=>$row, 'error'=>$this->Menu->getError()));
        } else {
            $this->display();
        }
    }

	/**
	 * 修改菜单
	 *
	 * @return HTML & JSON
	 */
	public function edit()
	{
		$menuid = intval($_GET['menuid']);
		if ($this->isPost()) {
			$result = false;
			// 验证并创建对象
			if ($data = $this->Menu->create()) {
				if ($this->Menu->edit($menuid, $data) !== false) {
					$result = true;
					$row = $this->Menu->find($menuid);
				}
			}
			echo json_encode(array('state'=>$result, 'data'=>$row, 'error'=>$this->Menu->getError()));
		} else {
			$menu = $this->Menu->find($menuid);
			$this->view->assign($menu);
			$this->display();
		}
	}

    /**
	 * 删除菜单
	 *
	 * @return JSON
	 */
    public function delete()
    {
        $result = $this->Menu->delete($_GET['id']);
        $json = $result ? array('state'=>true) : array('state'=>false, 'error'=>$this->Menu->getError());
        echo json_encode($json);
    }

	/**
	 * 修复菜单
	 *
	 * @return JSON
	 */
	public function repair()
	{
		$result = $this->Menu->repair();
		echo json_encode(array('state'=>$result, 'error'=>$this->Menu->getError()));
	}

	/**
	 * 菜单搜索表单
	 *
	 * @return HTML
	 */
	public function search()
	{
		$this->display();
	}

    /**
	 * 菜单分页查询与搜索入口
	 *
	 * @return JSON
	 */
    public function page()
    {
        $parentid = isset($_GET['parentid']) ? intval($_GET['parentid']) : null;
        if(isset($_GET['menuid'])) {
            $menu = $this->Menu->where('menuid='.$_GET['menuid'])->find();
            $parentid = $menu['parentid'];
        }

		// 判断是否包含搜索按钮请求，进行所有菜单搜索
		if (!isset($_GET['search'])) {
			$where['_string'] = is_null($parentid) ? "`parentid` IS NULL" : "`parentid`=$parentid";
		}
        if (isset($_GET['keywords']) && $_GET['keywords'])
            $where['name'] = array('like','%'.$_GET['keywords'].'%');
        if (isset($_GET['url']) && $_GET['url'])
            $where['url'] = array('like','%'.$_GET['url'].'%');

		$data = array();
        $result = $this->Menu->where($where)->order('`sort` ASC')->select();
		if ($result) {
			$total = count($result);
			$data = $result;
		}
        echo json_encode(array('total'=>$total, 'data'=>$data, 'parentid' => $parentid));
    }
}